/*****************************************************************
 * Author: DO HOANG HAI
 * Date of creation:
 * Last modification:
 * 
 * Description: This class provides functions to interact with
 * data in file Comments.XML. An object of this class represents
 * for an Comment object and corresponds to a Comment element in
 * the XML file.
 * 
 ****************************************************************/

using System;
using System.Collections.Generic;
using System.Text;
using Pbdms.Common;
using Pbdms.Common.DataObject;
using System.Collections;

namespace Pbdms.DataAccess.DataProvider
{
    interface ICommentProviderInterface : IProviderInterface
    {        
        Comment selectFromDB();
    }
    /// <summary>
    /// This class provides functions to interact with
    /// data in file Comments.XML. An object of this class represents
    /// for an Comment object and corresponds to a Comment element in
    /// the XML file.
    /// </summary>
    public class CommentProvider : ICommentProviderInterface                 
    {
        private const String FILENAME = "comment.xml";        
        private Comment commentObj;
            
        #region constructors
        /// <summary>
        /// create a commentProvider obj with new Comment Object to which
        /// it refer
        /// </summary>
        /// <param name="id"></param>
        public CommentProvider(String id){
            this.commentObj = new Comment(id) ;
        }

        /// <summary>
        /// create a CommentProvider object to refer to an existing Comment Obj
        /// </summary>
        /// <param name="cmtObj"></param>
        public CommentProvider(Comment cmtObj)
        {
            commentObj = cmtObj;
        }
       
        #endregion

        #region static methods
        /// <summary>
        /// create an instance of the class
        /// </summary>
        /// <param name="id">id of the item in database</param>
        public static CommentProvider create(String id) {
            return new CommentProvider(id );     
        }
        /// <summary>
        /// create a comment provider object for handling this comment object
        /// </summary>
        /// <param name="cmtObj"></param>
        /// <returns></returns>
        public static CommentProvider create(Comment cmtObj)
        {
            return new CommentProvider(cmtObj);
        }

        #endregion

        /// <summary>
        /// get the XPath of the this Comment with attribute id
        /// </summary>
        /// <returns></returns>
        private String getXPath()
        {
            return "/root/comment[@id='" + commentObj.Id + "']";
        }

        #region public methods

        /// <summary>
        /// get the comment object that this commentProvider is refering to
        /// </summary>
        /// <returns></returns>
        public Comment getCommentObject()
        {
            return commentObj;
        }

        /// <summary>
        /// update a specific value of an element in XML file
        /// </summary>
        /// <param name="name">name of the element</param>
        /// <param name="value"></param>
        public void updateElementToDB(String name, Object value)
        {
            XmlHelper.create(FILENAME).updateTypedElement(getXPath()+"/" + name, value);
        }

        /// <summary>
        /// update database with new values of attributes
        /// </summary>
        public void updateAllToDB()
        {
            updateElementToDB("summary", commentObj.Summary);
            updateElementToDB("pros", commentObj.Pros);
            updateElementToDB("cons", commentObj.Cons);
            updateElementToDB("rating", commentObj.Rating);            
            updateElementToDB("reviewFile", commentObj.ReviewFile);
            XmlHelper.create(FILENAME).deleteRange(getXPath()+"/tag",0);
            //fixed by chee hong
            // error due to no tags available
            if (commentObj.Tags != null)
            {
                foreach (String tag in commentObj.Tags)
                {
                    XmlHelper.create(FILENAME).appendChild(getXPath(), "tag", tag);
                }
            }
        }       

        /// <summary>
        /// create a new Comment object with data taken from database
        /// </summary>
        /// <param name="id">id of the object in XML file</param>
        /// <returns></returns>
        public Comment selectFromDB()
        {
            //select all value of the this comment in database
            Comment cmmObj = new Comment(commentObj.Id);
            cmmObj.Summary = XmlHelper.create(FILENAME).getInnerXML(getXPath() + "/summary");
            cmmObj.Pros = XmlHelper.create(FILENAME).getInnerXML(getXPath() + "/pros");
            cmmObj.Cons = XmlHelper.create(FILENAME).getInnerXML(getXPath() + "/cons");
            cmmObj.ReviewFile = XmlHelper.create(FILENAME).getInnerXML(getXPath() + "/reviewFile");
            String sRating = XmlHelper.create(FILENAME).getInnerXML(getXPath() + "/rating");            
            if (sRating.Equals(""))
                cmmObj.Rating = 0.0;
            else
                cmmObj.Rating = Double.Parse(sRating);

            cmmObj.Tags = XmlHelper.create(FILENAME).selectMultipleElementValues(getXPath() + "/tag");
                   
            //assign and return the object
            return cmmObj;
        }

        /// <summary>
        /// delete the item from the database
        /// </summary>
        public void deleteDB()
        {
            XmlHelper.create(FILENAME).deleteSelf(getXPath());
        }

        /// <summary>
        /// update the comment object with data from the database
        /// </summary>
        public void updateFromDB()
        {
            commentObj = selectFromDB();
        }
        #endregion

        public static void appendComment(Comment commentObj)
        {
            //construct the xml text
            StringBuilder sb = new StringBuilder();
            sb.Append("<comment id='" + commentObj.Id +"'>");
            sb.Append("<summary>"+ commentObj.Summary+"</summary>");
            sb.Append("<pros>"+ commentObj.Pros+"</pros>");
            sb.Append("<cons>" + commentObj.Cons+"</cons>");
            sb.Append("<rating>"+ commentObj.Rating.ToString()+"</rating>");
            sb.Append("<reviewFile>" + commentObj.ReviewFile + "</reviewFile>");
            if (commentObj.Tags !=null)
            {
                foreach (Object tag in commentObj.Tags)
                {
                     sb.Append("<tag>"+ tag.ToString()+"</tag>"); 
                }
            }
             sb.Append("</comment>");

            //append new child
            XmlHelper.create("comment.xml").appendChild("/root", sb.ToString());
        }
    }
}
